package com.bobi.tfca.ui.app

import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.databinding.ActivityMainBinding
import com.bobi.tfca.ui.mainnavigation.MainNavigationFragment
import com.bobi.tfca.ui.mainnavigation.home.HomeFragment
import com.bobi.tfca.ui.mainnavigation.message.chatroom.ChatRoomFragment
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView


class MainActivity : AppCompatActivity(){
    private lateinit var binding:ActivityMainBinding

    // 消息的高度
    private var messageRVHeight: Int = 0
    // 软键盘首次打开或是首次关闭
    private var keyboardFlag = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityMainBinding.inflate(layoutInflater)
        ViewModelProvider(this)[PublicViewModel::class.java]
        setContentView(binding.root)

        //申请通知权限
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1);
        }

        // 监听软键盘的显示和隐藏
        val decorView = window.decorView
        decorView.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            decorView.getWindowVisibleDisplayFrame(r)

            // 获取屏幕高度
            val screenHeight = decorView.height

            // 计算键盘高度
            val keyboardHeight = screenHeight - r.bottom

            // 获取当前显示的Fragment
            val navHostFragment: NavHostFragment? =
                supportFragmentManager.primaryNavigationFragment as NavHostFragment?

            val currentFragment =
                navHostFragment!!.getChildFragmentManager().primaryNavigationFragment

            // 判断当前Fragment是否是特定Fragment的实例
            if (currentFragment is ChatRoomFragment) {
                LogUtil.i("当前在ChatRoomFragment")
                // 进行相应的逻辑处理
                val fragmentView = currentFragment.view
                val messageRV = fragmentView!!.findViewById<RecyclerView>(R.id.messageRV)
                val linearLayout = fragmentView.findViewById<LinearLayout>(R.id.linearLayout4)
                messageRVHeight = messageRV.height

                // 如果键盘可见，调整 RecyclerView 的高度
                if (keyboardHeight > screenHeight / 4) { // 这里的阈值可以根据实际情况调整
                    // 如果是首次打开键盘，改变并拉到最后
                    messageRV.layoutParams.height = screenHeight - keyboardHeight - messageRV.top - linearLayout.height - 140
                    if (!keyboardFlag) {
                        // 滑动到最后一个位置
                        val lastItemPosition = messageRV.adapter!!.itemCount - 1
                        if (lastItemPosition >= 0) {
                            messageRV.scrollToPosition(lastItemPosition)
                        }
                    }
                    keyboardFlag = true
                } else {
                    // 如果键盘关闭，恢复高度并拉到最后
                    if (keyboardFlag) {
                        // 如果键盘隐藏，恢复 RecyclerView 的原始高度
                        messageRV.layoutParams.height = messageRVHeight
                        // 滑动到最后一个位置
                        val lastItemPosition = messageRV.adapter!!.itemCount - 1
                        if (lastItemPosition >= 0) {
                            messageRV.scrollToPosition(lastItemPosition)
                        }
                        keyboardFlag = false
                    }
                }

            } else if (currentFragment is MainNavigationFragment) {
                LogUtil.i("当前在MainNavigationFragment")
                // 如果键盘可见，调整 RecyclerView 的高度
                val bottomNavigationView =
                    findViewById<BottomNavigationView>(R.id.bottom_nav)
                if (keyboardHeight > screenHeight / 4) { // 这里的阈值可以根据实际情况调整
                    // 如果是首次打开键盘
                    if (!keyboardFlag) {
                        if (bottomNavigationView != null) {
                            bottomNavigationView.visibility = View.GONE
                        }
                    }
                    keyboardFlag = true
                } else {
                    // 如果键盘关闭，恢复高度并拉到最后
                    if (keyboardFlag) {
                        bottomNavigationView.visibility = View.VISIBLE
                        keyboardFlag = false
                    }
                }
            } else {
                LogUtil.i("Fragment type: ${currentFragment?.javaClass?.simpleName}")
            }
        }
    }
}
